
<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>pymatgen.alchemy.filters &#8212; pymatgen 2020.7.3 documentation</title>
    <link rel="stylesheet" href="../../../_static/basic.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
    <script src="../../../_static/jquery.js"></script>
    <script src="../../../_static/underscore.js"></script>
    <script src="../../../_static/doctools.js"></script>
    <script src="../../../_static/language_data.js"></script>
    <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />
 
<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-33990148-1']);
  _gaq.push(['_trackPageview']);
</script>

  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">pymatgen 2020.7.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../../pymatgen.html" accesskey="U">pymatgen</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">pymatgen.alchemy.filters</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
          <div class="body" role="main">
            
  <h1>Source code for pymatgen.alchemy.filters</h1><div class="highlight"><pre>
<span></span><span class="c1"># coding: utf-8</span>
<span class="c1"># Copyright (c) Pymatgen Development Team.</span>
<span class="c1"># Distributed under the terms of the MIT License.</span>

<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">This module defines filters for Transmuter object.</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">import</span> <span class="nn">abc</span>

<span class="kn">from</span> <span class="nn">pymatgen.core.periodic_table</span> <span class="kn">import</span> <span class="n">get_el_sp</span>
<span class="kn">from</span> <span class="nn">monty.json</span> <span class="kn">import</span> <span class="n">MSONable</span>
<span class="kn">from</span> <span class="nn">pymatgen.analysis.structure_matcher</span> <span class="kn">import</span> <span class="n">StructureMatcher</span><span class="p">,</span> \
    <span class="n">ElementComparator</span>
<span class="kn">from</span> <span class="nn">pymatgen.symmetry.analyzer</span> <span class="kn">import</span> <span class="n">SpacegroupAnalyzer</span>

<span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">defaultdict</span>

<span class="n">__author__</span> <span class="o">=</span> <span class="s2">&quot;Will Richards, Shyue Ping Ong, Stephen Dacek&quot;</span>
<span class="n">__copyright__</span> <span class="o">=</span> <span class="s2">&quot;Copyright 2011, The Materials Project&quot;</span>
<span class="n">__version__</span> <span class="o">=</span> <span class="s2">&quot;1.0&quot;</span>
<span class="n">__maintainer__</span> <span class="o">=</span> <span class="s2">&quot;Will Richards&quot;</span>
<span class="n">__email__</span> <span class="o">=</span> <span class="s2">&quot;wrichards@mit.edu&quot;</span>
<span class="n">__date__</span> <span class="o">=</span> <span class="s2">&quot;Sep 25, 2012&quot;</span>


<div class="viewcode-block" id="AbstractStructureFilter"><a class="viewcode-back" href="../../../pymatgen.alchemy.filters.html#pymatgen.alchemy.filters.AbstractStructureFilter">[docs]</a><span class="k">class</span> <span class="nc">AbstractStructureFilter</span><span class="p">(</span><span class="n">MSONable</span><span class="p">,</span> <span class="n">metaclass</span><span class="o">=</span><span class="n">abc</span><span class="o">.</span><span class="n">ABCMeta</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    AbstractStructureFilter that defines an API to perform testing of</span>
<span class="sd">    Structures. Structures that return True to a test are retained during</span>
<span class="sd">    transmutation while those that return False are removed.</span>
<span class="sd">    &quot;&quot;&quot;</span>

<div class="viewcode-block" id="AbstractStructureFilter.test"><a class="viewcode-back" href="../../../pymatgen.alchemy.filters.html#pymatgen.alchemy.filters.AbstractStructureFilter.test">[docs]</a>    <span class="nd">@abc</span><span class="o">.</span><span class="n">abstractmethod</span>
    <span class="k">def</span> <span class="nf">test</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Method to execute the test.</span>

<span class="sd">        Args:</span>
<span class="sd">            structure (Structure): Input structure to test</span>

<span class="sd">        Returns:</span>
<span class="sd">            (bool) Structures that return true are kept in the Transmuter</span>
<span class="sd">            object during filtering.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span></div></div>


<div class="viewcode-block" id="ContainsSpecieFilter"><a class="viewcode-back" href="../../../pymatgen.alchemy.filters.html#pymatgen.alchemy.filters.ContainsSpecieFilter">[docs]</a><span class="k">class</span> <span class="nc">ContainsSpecieFilter</span><span class="p">(</span><span class="n">AbstractStructureFilter</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Filter for structures containing certain elements or species.</span>
<span class="sd">    By default compares by atomic number.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">species</span><span class="p">,</span> <span class="n">strict_compare</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">AND</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">exclude</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Args:</span>
<span class="sd">            species ([Specie/Element]): list of species to look for</span>
<span class="sd">            AND: whether all species must be present to pass (or fail) filter.</span>
<span class="sd">            strict_compare: if true, compares objects by specie or element</span>
<span class="sd">                object if false, compares atomic number</span>
<span class="sd">            exclude: If true, returns false for any structures with the specie</span>
<span class="sd">                (excludes them from the Transmuter)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_species</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="n">get_el_sp</span><span class="p">,</span> <span class="n">species</span><span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_strict</span> <span class="o">=</span> <span class="n">strict_compare</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_AND</span> <span class="o">=</span> <span class="n">AND</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_exclude</span> <span class="o">=</span> <span class="n">exclude</span>

<div class="viewcode-block" id="ContainsSpecieFilter.test"><a class="viewcode-back" href="../../../pymatgen.alchemy.filters.html#pymatgen.alchemy.filters.ContainsSpecieFilter.test">[docs]</a>    <span class="k">def</span> <span class="nf">test</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Method to execute the test.</span>

<span class="sd">        Returns: True if structure do not contain specified species.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># set up lists to compare</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_strict</span><span class="p">:</span>
            <span class="c1"># compare by atomic number</span>
            <span class="n">filter_set</span> <span class="o">=</span> <span class="nb">set</span><span class="p">([</span><span class="n">sp</span><span class="o">.</span><span class="n">Z</span> <span class="k">for</span> <span class="n">sp</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_species</span><span class="p">])</span>
            <span class="n">structure_set</span> <span class="o">=</span> <span class="nb">set</span><span class="p">([</span><span class="n">sp</span><span class="o">.</span><span class="n">Z</span> <span class="k">for</span> <span class="n">sp</span> <span class="ow">in</span> <span class="n">structure</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">elements</span><span class="p">])</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># compare by specie or element object</span>
            <span class="n">filter_set</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_species</span><span class="p">)</span>
            <span class="n">structure_set</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">structure</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">elements</span><span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_AND</span> <span class="ow">and</span> <span class="n">filter_set</span> <span class="o">&lt;=</span> <span class="n">structure_set</span><span class="p">:</span>
            <span class="c1"># return true if we aren&#39;t excluding since all are in structure</span>
            <span class="k">return</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_exclude</span>
        <span class="k">elif</span> <span class="p">(</span><span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_AND</span><span class="p">)</span> <span class="ow">and</span> <span class="n">filter_set</span> <span class="o">&amp;</span> <span class="n">structure_set</span><span class="p">:</span>
            <span class="c1"># return true if we aren&#39;t excluding since one is in structure</span>
            <span class="k">return</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_exclude</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># return false if we aren&#39;t excluding otherwise</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_exclude</span></div>

    <span class="k">def</span> <span class="fm">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="s2">&quot;ContainsSpecieFilter with parameters:&quot;</span><span class="p">,</span>
                          <span class="s2">&quot;species = </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_species</span><span class="p">),</span>
                          <span class="s2">&quot;strict_compare = </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_strict</span><span class="p">),</span>
                          <span class="s2">&quot;AND = </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_AND</span><span class="p">),</span>
                          <span class="s2">&quot;exclude = </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_exclude</span><span class="p">)])</span>

<div class="viewcode-block" id="ContainsSpecieFilter.as_dict"><a class="viewcode-back" href="../../../pymatgen.alchemy.filters.html#pymatgen.alchemy.filters.ContainsSpecieFilter.as_dict">[docs]</a>    <span class="k">def</span> <span class="nf">as_dict</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns: MSONAble dict</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">{</span><span class="s2">&quot;version&quot;</span><span class="p">:</span> <span class="n">__version__</span><span class="p">,</span> <span class="s2">&quot;@module&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__module__</span><span class="p">,</span>
                <span class="s2">&quot;@class&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
                <span class="s2">&quot;init_args&quot;</span><span class="p">:</span> <span class="p">{</span><span class="s2">&quot;species&quot;</span><span class="p">:</span> <span class="p">[</span><span class="nb">str</span><span class="p">(</span><span class="n">sp</span><span class="p">)</span> <span class="k">for</span> <span class="n">sp</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_species</span><span class="p">],</span>
                              <span class="s2">&quot;strict_compare&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_strict</span><span class="p">,</span>
                              <span class="s2">&quot;AND&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_AND</span><span class="p">,</span>
                              <span class="s2">&quot;exclude&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_exclude</span><span class="p">}}</span></div>

<div class="viewcode-block" id="ContainsSpecieFilter.from_dict"><a class="viewcode-back" href="../../../pymatgen.alchemy.filters.html#pymatgen.alchemy.filters.ContainsSpecieFilter.from_dict">[docs]</a>    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">from_dict</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">d</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Args:</span>
<span class="sd">            d (dict): Dict representation</span>

<span class="sd">        Returns:</span>
<span class="sd">            Filter</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">cls</span><span class="p">(</span><span class="o">**</span><span class="n">d</span><span class="p">[</span><span class="s2">&quot;init_args&quot;</span><span class="p">])</span></div></div>


<div class="viewcode-block" id="SpecieProximityFilter"><a class="viewcode-back" href="../../../pymatgen.alchemy.filters.html#pymatgen.alchemy.filters.SpecieProximityFilter">[docs]</a><span class="k">class</span> <span class="nc">SpecieProximityFilter</span><span class="p">(</span><span class="n">AbstractStructureFilter</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    This filter removes structures that have certain species that are too close</span>
<span class="sd">    together.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">specie_and_min_dist_dict</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Args:</span>
<span class="sd">            specie_and_min_dist_dict (dict): A species string to float mapping. For</span>
<span class="sd">                example, {&quot;Na+&quot;: 1} means that all Na+ ions must be at least 1</span>
<span class="sd">                Angstrom away from each other. Multiple species criteria can be</span>
<span class="sd">                applied. Note that the testing is done based on the actual object</span>
<span class="sd">                . If you have a structure with Element, you must use {&quot;Na&quot;:1}</span>
<span class="sd">                instead to filter based on Element and not Specie.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">specie_and_min_dist</span> <span class="o">=</span> <span class="p">{</span><span class="n">get_el_sp</span><span class="p">(</span><span class="n">k</span><span class="p">):</span> <span class="n">v</span>
                                    <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span>
                                    <span class="ow">in</span> <span class="n">specie_and_min_dist_dict</span><span class="o">.</span><span class="n">items</span><span class="p">()}</span>

<div class="viewcode-block" id="SpecieProximityFilter.test"><a class="viewcode-back" href="../../../pymatgen.alchemy.filters.html#pymatgen.alchemy.filters.SpecieProximityFilter.test">[docs]</a>    <span class="k">def</span> <span class="nf">test</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Method to execute the test.</span>

<span class="sd">        Args:</span>
<span class="sd">            structure (Structure): Input structure to test</span>

<span class="sd">        Returns: True if structure does not contain species within specified</span>
<span class="sd">            distances.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">all_species</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">specie_and_min_dist</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span>
        <span class="k">for</span> <span class="n">site</span> <span class="ow">in</span> <span class="n">structure</span><span class="p">:</span>
            <span class="n">species</span> <span class="o">=</span> <span class="n">site</span><span class="o">.</span><span class="n">species</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span>
            <span class="n">sp_to_test</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">species</span><span class="p">)</span><span class="o">.</span><span class="n">intersection</span><span class="p">(</span><span class="n">all_species</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">sp_to_test</span><span class="p">:</span>
                <span class="n">max_r</span> <span class="o">=</span> <span class="nb">max</span><span class="p">([</span><span class="bp">self</span><span class="o">.</span><span class="n">specie_and_min_dist</span><span class="p">[</span><span class="n">sp</span><span class="p">]</span>
                             <span class="k">for</span> <span class="n">sp</span> <span class="ow">in</span> <span class="n">sp_to_test</span><span class="p">])</span>
                <span class="n">nn</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">get_neighbors</span><span class="p">(</span><span class="n">site</span><span class="p">,</span> <span class="n">max_r</span><span class="p">)</span>
                <span class="k">for</span> <span class="n">sp</span> <span class="ow">in</span> <span class="n">sp_to_test</span><span class="p">:</span>
                    <span class="k">for</span> <span class="n">nnsite</span><span class="p">,</span> <span class="n">dist</span><span class="p">,</span> <span class="o">*</span><span class="n">_</span> <span class="ow">in</span> <span class="n">nn</span><span class="p">:</span>
                        <span class="k">if</span> <span class="n">sp</span> <span class="ow">in</span> <span class="n">nnsite</span><span class="o">.</span><span class="n">species</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
                            <span class="k">if</span> <span class="n">dist</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">specie_and_min_dist</span><span class="p">[</span><span class="n">sp</span><span class="p">]:</span>
                                <span class="k">return</span> <span class="kc">False</span>
        <span class="k">return</span> <span class="kc">True</span></div>

<div class="viewcode-block" id="SpecieProximityFilter.as_dict"><a class="viewcode-back" href="../../../pymatgen.alchemy.filters.html#pymatgen.alchemy.filters.SpecieProximityFilter.as_dict">[docs]</a>    <span class="k">def</span> <span class="nf">as_dict</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns: MSONable dict</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">{</span><span class="s2">&quot;version&quot;</span><span class="p">:</span> <span class="n">__version__</span><span class="p">,</span> <span class="s2">&quot;@module&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__module__</span><span class="p">,</span>
                <span class="s2">&quot;@class&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
                <span class="s2">&quot;init_args&quot;</span><span class="p">:</span> <span class="p">{</span><span class="s2">&quot;specie_and_min_dist_dict&quot;</span><span class="p">:</span> <span class="p">{</span><span class="nb">str</span><span class="p">(</span><span class="n">sp</span><span class="p">):</span> <span class="n">v</span>
                                                           <span class="k">for</span> <span class="n">sp</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">specie_and_min_dist</span><span class="o">.</span><span class="n">items</span><span class="p">()}}}</span></div>

<div class="viewcode-block" id="SpecieProximityFilter.from_dict"><a class="viewcode-back" href="../../../pymatgen.alchemy.filters.html#pymatgen.alchemy.filters.SpecieProximityFilter.from_dict">[docs]</a>    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">from_dict</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">d</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Args:</span>
<span class="sd">            d (dict): Dict representation</span>

<span class="sd">        Returns:</span>
<span class="sd">            Filter</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">cls</span><span class="p">(</span><span class="o">**</span><span class="n">d</span><span class="p">[</span><span class="s2">&quot;init_args&quot;</span><span class="p">])</span></div></div>


<div class="viewcode-block" id="RemoveDuplicatesFilter"><a class="viewcode-back" href="../../../pymatgen.alchemy.filters.html#pymatgen.alchemy.filters.RemoveDuplicatesFilter">[docs]</a><span class="k">class</span> <span class="nc">RemoveDuplicatesFilter</span><span class="p">(</span><span class="n">AbstractStructureFilter</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    This filter removes exact duplicate structures from the transmuter.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure_matcher</span><span class="o">=</span><span class="n">StructureMatcher</span><span class="p">(</span><span class="n">comparator</span><span class="o">=</span><span class="n">ElementComparator</span><span class="p">()),</span> <span class="n">symprec</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Remove duplicate structures based on the structure matcher</span>
<span class="sd">        and symmetry (if symprec is given).</span>

<span class="sd">        Args:</span>
<span class="sd">            structure_matcher: Provides a structure matcher to be used for</span>
<span class="sd">                structure comparison.</span>
<span class="sd">            symprec: The precision in the symmetry finder algorithm if None (</span>
<span class="sd">                default value), no symmetry check is performed and only the</span>
<span class="sd">                structure matcher is used. A recommended value is 1e-5.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">symprec</span> <span class="o">=</span> <span class="n">symprec</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">structure_list</span> <span class="o">=</span> <span class="n">defaultdict</span><span class="p">(</span><span class="nb">list</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">structure_matcher</span><span class="p">,</span> <span class="nb">dict</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">structure_matcher</span> <span class="o">=</span> <span class="n">StructureMatcher</span><span class="o">.</span><span class="n">from_dict</span><span class="p">(</span><span class="n">structure_matcher</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">structure_matcher</span> <span class="o">=</span> <span class="n">structure_matcher</span>

<div class="viewcode-block" id="RemoveDuplicatesFilter.test"><a class="viewcode-back" href="../../../pymatgen.alchemy.filters.html#pymatgen.alchemy.filters.RemoveDuplicatesFilter.test">[docs]</a>    <span class="k">def</span> <span class="nf">test</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Args:</span>
<span class="sd">            structure (Structure): Input structure to test</span>

<span class="sd">        Returns: True if structure is not in list.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">h</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">structure_matcher</span><span class="o">.</span><span class="n">_comparator</span><span class="o">.</span><span class="n">get_hash</span><span class="p">(</span><span class="n">structure</span><span class="o">.</span><span class="n">composition</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">structure_list</span><span class="p">[</span><span class="n">h</span><span class="p">]:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">structure_list</span><span class="p">[</span><span class="n">h</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">structure</span><span class="p">)</span>
            <span class="k">return</span> <span class="kc">True</span>

        <span class="k">def</span> <span class="nf">get_sg</span><span class="p">(</span><span class="n">s</span><span class="p">):</span>
            <span class="n">finder</span> <span class="o">=</span> <span class="n">SpacegroupAnalyzer</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">symprec</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">symprec</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">finder</span><span class="o">.</span><span class="n">get_space_group_number</span><span class="p">()</span>

        <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">structure_list</span><span class="p">[</span><span class="n">h</span><span class="p">]:</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">symprec</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> \
                    <span class="n">get_sg</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> <span class="o">==</span> <span class="n">get_sg</span><span class="p">(</span><span class="n">structure</span><span class="p">):</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">structure_matcher</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">structure</span><span class="p">):</span>
                    <span class="k">return</span> <span class="kc">False</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">structure_list</span><span class="p">[</span><span class="n">h</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">structure</span><span class="p">)</span>
        <span class="k">return</span> <span class="kc">True</span></div></div>


<div class="viewcode-block" id="RemoveExistingFilter"><a class="viewcode-back" href="../../../pymatgen.alchemy.filters.html#pymatgen.alchemy.filters.RemoveExistingFilter">[docs]</a><span class="k">class</span> <span class="nc">RemoveExistingFilter</span><span class="p">(</span><span class="n">AbstractStructureFilter</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    This filter removes structures existing in a given list from the transmuter.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">existing_structures</span><span class="p">,</span> <span class="n">structure_matcher</span><span class="o">=</span><span class="n">StructureMatcher</span><span class="p">(</span><span class="n">comparator</span><span class="o">=</span><span class="n">ElementComparator</span><span class="p">()),</span>
                 <span class="n">symprec</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Remove existing structures based on the structure matcher</span>
<span class="sd">        and symmetry (if symprec is given).</span>

<span class="sd">        Args:</span>
<span class="sd">            existing_structures: List of existing structures to compare with</span>
<span class="sd">            structure_matcher: Provides a structure matcher to be used for</span>
<span class="sd">                structure comparison.</span>
<span class="sd">            symprec: The precision in the symmetry finder algorithm if None (</span>
<span class="sd">                default value), no symmetry check is performed and only the</span>
<span class="sd">                structure matcher is used. A recommended value is 1e-5.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">symprec</span> <span class="o">=</span> <span class="n">symprec</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">structure_list</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">existing_structures</span> <span class="o">=</span> <span class="n">existing_structures</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">structure_matcher</span><span class="p">,</span> <span class="nb">dict</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">structure_matcher</span> <span class="o">=</span> <span class="n">StructureMatcher</span><span class="o">.</span><span class="n">from_dict</span><span class="p">(</span><span class="n">structure_matcher</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">structure_matcher</span> <span class="o">=</span> <span class="n">structure_matcher</span>

<div class="viewcode-block" id="RemoveExistingFilter.test"><a class="viewcode-back" href="../../../pymatgen.alchemy.filters.html#pymatgen.alchemy.filters.RemoveExistingFilter.test">[docs]</a>    <span class="k">def</span> <span class="nf">test</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Method to execute the test.</span>

<span class="sd">        Args:</span>
<span class="sd">            structure (Structure): Input structure to test</span>

<span class="sd">        Returns: True if structure is not in existing list.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">def</span> <span class="nf">get_sg</span><span class="p">(</span><span class="n">s</span><span class="p">):</span>
            <span class="n">finder</span> <span class="o">=</span> <span class="n">SpacegroupAnalyzer</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">symprec</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">symprec</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">finder</span><span class="o">.</span><span class="n">get_space_group_number</span><span class="p">()</span>

        <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">existing_structures</span><span class="p">:</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">structure_matcher</span><span class="o">.</span><span class="n">_comparator</span><span class="o">.</span><span class="n">get_hash</span><span class="p">(</span><span class="n">structure</span><span class="o">.</span><span class="n">composition</span><span class="p">)</span> <span class="o">==</span> \
                    <span class="bp">self</span><span class="o">.</span><span class="n">structure_matcher</span><span class="o">.</span><span class="n">_comparator</span><span class="o">.</span><span class="n">get_hash</span><span class="p">(</span><span class="n">s</span><span class="o">.</span><span class="n">composition</span><span class="p">):</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">symprec</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> \
                        <span class="n">get_sg</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> <span class="o">==</span> <span class="n">get_sg</span><span class="p">(</span><span class="n">structure</span><span class="p">):</span>
                    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">structure_matcher</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">structure</span><span class="p">):</span>
                        <span class="k">return</span> <span class="kc">False</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">structure_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">structure</span><span class="p">)</span>
        <span class="k">return</span> <span class="kc">True</span></div>

<div class="viewcode-block" id="RemoveExistingFilter.as_dict"><a class="viewcode-back" href="../../../pymatgen.alchemy.filters.html#pymatgen.alchemy.filters.RemoveExistingFilter.as_dict">[docs]</a>    <span class="k">def</span> <span class="nf">as_dict</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns: MSONable dict</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">{</span><span class="s2">&quot;version&quot;</span><span class="p">:</span> <span class="n">__version__</span><span class="p">,</span> <span class="s2">&quot;@module&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__module__</span><span class="p">,</span>
                <span class="s2">&quot;@class&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
                <span class="s2">&quot;init_args&quot;</span><span class="p">:</span> <span class="p">{</span><span class="s2">&quot;structure_matcher&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">structure_matcher</span><span class="o">.</span><span class="n">as_dict</span><span class="p">()}}</span></div></div>


<div class="viewcode-block" id="ChargeBalanceFilter"><a class="viewcode-back" href="../../../pymatgen.alchemy.filters.html#pymatgen.alchemy.filters.ChargeBalanceFilter">[docs]</a><span class="k">class</span> <span class="nc">ChargeBalanceFilter</span><span class="p">(</span><span class="n">AbstractStructureFilter</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    This filter removes structures that are not charge balanced from the</span>
<span class="sd">    transmuter. This only works if the structure is oxidation state</span>
<span class="sd">    decorated, as structures with only elemental sites are automatically</span>
<span class="sd">    assumed to have net charge of 0.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        No args required.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">pass</span>

<div class="viewcode-block" id="ChargeBalanceFilter.test"><a class="viewcode-back" href="../../../pymatgen.alchemy.filters.html#pymatgen.alchemy.filters.ChargeBalanceFilter.test">[docs]</a>    <span class="k">def</span> <span class="nf">test</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Method to execute the test.</span>

<span class="sd">        Args:</span>
<span class="sd">            structure (Structure): Input structure to test</span>

<span class="sd">        Returns: True if structure is neutral.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">structure</span><span class="o">.</span><span class="n">charge</span> <span class="o">==</span> <span class="mf">0.0</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">True</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">False</span></div></div>


<div class="viewcode-block" id="SpeciesMaxDistFilter"><a class="viewcode-back" href="../../../pymatgen.alchemy.filters.html#pymatgen.alchemy.filters.SpeciesMaxDistFilter">[docs]</a><span class="k">class</span> <span class="nc">SpeciesMaxDistFilter</span><span class="p">(</span><span class="n">AbstractStructureFilter</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    This filter removes structures that do have two particular species that are</span>
<span class="sd">    not nearest neighbors by a predefined max_dist. For instance, if you are</span>
<span class="sd">    analyzing Li battery materials, you would expect that each Li+ would be</span>
<span class="sd">    nearest neighbor to lower oxidation state transition metal for</span>
<span class="sd">    electrostatic reasons. This only works if the structure is oxidation state</span>
<span class="sd">    decorated, as structures with only elemental sites are automatically</span>
<span class="sd">    assumed to have net charge of 0.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sp1</span><span class="p">,</span> <span class="n">sp2</span><span class="p">,</span> <span class="n">max_dist</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Args:</span>
<span class="sd">            sp1 (Specie): First specie</span>
<span class="sd">            sp2 (Specie): Second specie</span>
<span class="sd">            max_dist (float): Maximum distance between species.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">sp1</span> <span class="o">=</span> <span class="n">get_el_sp</span><span class="p">(</span><span class="n">sp1</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">sp2</span> <span class="o">=</span> <span class="n">get_el_sp</span><span class="p">(</span><span class="n">sp2</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">max_dist</span> <span class="o">=</span> <span class="n">max_dist</span>

<div class="viewcode-block" id="SpeciesMaxDistFilter.test"><a class="viewcode-back" href="../../../pymatgen.alchemy.filters.html#pymatgen.alchemy.filters.SpeciesMaxDistFilter.test">[docs]</a>    <span class="k">def</span> <span class="nf">test</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Method to execute the test.</span>

<span class="sd">        Args:</span>
<span class="sd">            structure (Structure): Input structure to test</span>

<span class="sd">        Returns: True if structure does not contain the two species are distances</span>
<span class="sd">            greater than max_dist.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">sp1_indices</span> <span class="o">=</span> <span class="p">[</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">site</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">structure</span><span class="p">)</span> <span class="k">if</span>
                       <span class="n">site</span><span class="o">.</span><span class="n">specie</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">sp1</span><span class="p">]</span>
        <span class="n">sp2_indices</span> <span class="o">=</span> <span class="p">[</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">site</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">structure</span><span class="p">)</span> <span class="k">if</span>
                       <span class="n">site</span><span class="o">.</span><span class="n">specie</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">sp2</span><span class="p">]</span>
        <span class="n">fcoords</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">frac_coords</span>
        <span class="n">fcoords1</span> <span class="o">=</span> <span class="n">fcoords</span><span class="p">[</span><span class="n">sp1_indices</span><span class="p">,</span> <span class="p">:]</span>
        <span class="n">fcoords2</span> <span class="o">=</span> <span class="n">fcoords</span><span class="p">[</span><span class="n">sp2_indices</span><span class="p">,</span> <span class="p">:]</span>
        <span class="n">lattice</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">lattice</span>
        <span class="n">dists</span> <span class="o">=</span> <span class="n">lattice</span><span class="o">.</span><span class="n">get_all_distances</span><span class="p">(</span><span class="n">fcoords1</span><span class="p">,</span> <span class="n">fcoords2</span><span class="p">)</span>
        <span class="k">return</span> <span class="nb">all</span><span class="p">([</span><span class="nb">any</span><span class="p">(</span><span class="n">row</span><span class="p">)</span> <span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">dists</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_dist</span><span class="p">])</span></div></div>
</pre></div>

            <div class="clearer"></div>
          </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">pymatgen 2020.7.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../../pymatgen.html" >pymatgen</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">pymatgen.alchemy.filters</a></li> 
      </ul>
    </div>

    <div class="footer" role="contentinfo">
        &#169; Copyright 2011, Pymatgen Development Team.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 3.1.2.
    </div>
<div class="footer">This page uses <a href="http://analytics.google.com/">
Google Analytics</a> to collect statistics. You can disable it by blocking
the JavaScript coming from www.google-analytics.com.
<script type="text/javascript">
  (function() {
    var ga = document.createElement('script');
    ga.src = ('https:' == document.location.protocol ?
              'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    ga.setAttribute('async', 'true');
    document.documentElement.firstChild.appendChild(ga);
  })();
</script>
</div>

  </body>
</html>